# -*- coding: utf-8 -*-
# standard
import os
import sys
import timeit

# third
import csv
import numpy as np
from pydantic import BaseModel
from pydantic import Field

# local
_P_PATH =  os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
if _P_PATH not in sys.path:
    sys.path.append(_P_PATH)
from models.scp_files import *


"""
@Title:   
@File: csv_model.py
@Author: walle 2023年12日07日 16时28分14秒
@Version: 1.0.0
@Desc: 
"""


class Media(BaseModel):
    """
    @Title: 媒体模型
    @File: media_model.py
    @Author: walle 2023年12月07日 16时28分14秒
    @Version: 1.0.0
    @Desc: 
    """
    video_id: str = Field(..., description="视频id")
    start_time: str = Field(..., description="视频开始时间")
    end_time: str = Field(..., description="视频开始时间")
    
    def __init__(self, video_id: str, start_time: str, end_time: str):
        super().__init__(video_id=video_id, start_time=start_time, end_time=end_time)


def read_csv_by_stand_lib(file_path: str):
    """
    @Title: 使用标准库读取csv文件， 慢
    @File: read_csv.py
    @Author: walle 2023年12月07日 16时28分14秒
    @Version: 1.0.0
    @Desc: 
    """
    with open(file_path, 'r', encoding='utf-8') as f:
        reader = csv.reader(f)
        for line in reader:
            print(line)


def read_csv_by_numpy(file_path: str):
    """
    @Title: 使用numpy读取csv文件，快
    @File: read_csv.py
    @Author: walle 2023年12月07日 16时28分14秒
    @Version: 1.0.0
    @Desc: 
    耗时: 3.485090699978173
    """
    data = np.genfromtxt(file_path, usecols=(0, 1, 2), delimiter=',', dtype=str)
    # 字典的方式更快
    data = [{
        "video_id": x[0],
        "start_time": x[1],
        "end_time": x[2],
        } for x in data]
    # 对象的方式更方便
    # data = [Media(*x.tolist()) for x in data]
    return data


def get_all_medias_from_csv():
    csv_dir = os.path.join(_P_PATH, "csv")
    csv_files = [os.path.join(csv_dir, x) for x in os.listdir(csv_dir) if x.endswith(".csv")]
    result = {}
    for csv_file in csv_files:
        csv_lines = np.genfromtxt(csv_file, usecols=(0, 1, 2), delimiter=',', dtype=str) 
        for line in csv_lines:
            video_id = line[0]
            video_info = result.get(video_id, {})
            if len(video_info) == 0:
                video_info['key'] = video_id
                video_info['clips'] = []
            video_info['clips'].append({
                "start_time": line[1],
                "end_time": line[2],
            })
            result[video_id] = video_info
    return result


def extract_medias_to_db():
    """
    从csv目录加载文件写入数据库，只需要在初始化数据库时运行一次
    2023-12-8 已初始化完毕
    """
    data: dict = get_all_medias_from_csv()
    logger.debug(f"load {len(data)} medias from csv")
    db_session = new_db_session()
    for media_info in data.values():
        key = media_info['key']
        page_url=f"https://www.youtube.com/watch?v={key}"
        media = MediaFileOrm(key=key, page_url=page_url)
        db_session.add(media)
        for clip_info in media_info['clips']:
            start_time = clip_info['start_time']
            end_time = clip_info['end_time']
            clip = ClipOrm(media_key=key, start_time=start_time, end_time=end_time)
            db_session.add(clip)
    db_session.commit()
    db_session.close()
        


if __name__ == '__main__':
    extract_medias_to_db()
    pass